#include <stdio.h>
#include <string.h>
#include "network.h"
#include <signal.h>
#include <sys/time.h>

int network_test(single *single)
{
	printf("network_test--->>>\n");
	FILE *fp = popen("sh/ping.sh", "r");
    char buffer[1024] = {0};
    fgets(buffer, sizeof(buffer), fp);
    char *buf=buffer;
    char *outer_ptr=NULL;
    char *inner_ptr=NULL;
    char * p;
    char * q;
    int i=1024;
    while (buffer[i]!='#')
    {
    buffer[i] = '\0';
    i--;
    if(i < 0)
    	break;
    }
    i=0;
    int j =0;
    while((p = strtok_r(buf, "#", &outer_ptr))!=NULL) {
//    printf ("%s\n",p);
    buf=p;
    j=0;
    while((q=strtok_r(buf, "/", &inner_ptr))!=NULL)
    {
//        printf ("%s\n",q);
        switch (j)
             {
             case 0:
             single[i].package=atoi(q);
                 break;
             case 1:
             single[i].min=atoi(q);
                  break;
             case 2:
            single[i].ave=atoi(q);
                 break;
             case 3:
            single[i].max=atoi(q);
                 break;
             }
        j++;

        buf=NULL;
    }
        i++;
    buf=NULL;
    }
    pclose(fp);
  //  int k=0;
  //  for( k=0;k<i;k++)
 //   {
 //   printf("SINGLE:%s\tMAC:%s\tSSID:%s\n",wifi[k].single,wifi[k].mac,wifi[k].ssid);
 //   }
    printf("network_test<<<---\n");
    return i;

}

int network_analysis ()
{

	printf("network_analysis--->>>\n");
	time_t timep;
	time (&timep);
	printf("The begin time is %s \n",ctime(&timep));
    single network[10];
    int rece = 0;
    int i = network_test(network) ;
    int k=0;
    for( k=0;k<i;k++)
    {
    rece=rece+network[k].package;
    }
    if(rece > 6)
    {
    	printf("The network is OK\n");
    	printf("network_analysis<<<---\n");
    	return 0;
    }else
    {
    	printf("The network is ERROR\n");
    	printf("network_analysis<<<---\n");
    	return -1;
    }


}

void network_start(int time_sec)
{
	struct itimerval value;
	value.it_value.tv_sec=time_sec;    /* 定时1.5秒 */
	value.it_value.tv_usec=0;
	value.it_interval.tv_sec=time_sec;    /* 定时1.5秒 */
	value.it_interval.tv_usec=0;
    signal(SIGALRM,test_event);
    setitimer(ITIMER_REAL, &value, NULL); /* 定时开始 */
    printf("network test start\n");
    printf("The test circle time is %d s\n",time_sec);
}


int wifi_scan (stu *wifi)
{
	printf("wifi_scan--->>>\n");
      FILE *fp = popen("cat src/b.txt", "r");
    char buffer[2000] = {0};
    char test[] = "fsafa";
    fgets(buffer, sizeof(buffer), fp);
//    printf("%s", buffer);
    char *buf=buffer;
    char *outer_ptr=NULL;
    char *inner_ptr=NULL;
    char * p;
    char * q;
    int i=2000;
    while (buffer[i]!='#')
    {
    buffer[i] = '\0';
    i--;
    if(i < 0)
       	break;
    }
    i=0;
    int j =0;
    while((p = strtok_r(buf, "##", &outer_ptr))!=NULL) {
  //  printf ("%s\n",p);
    buf=p;
    j=0;
    while((q=strtok_r(buf, "**", &inner_ptr))!=NULL)
    {
    //	printf ("%s\n",q);

        switch (j)
             {
             case 0:
     //       	 printf ("q=====%s\n",q);
      //      	 memset((void *)wifi[i].single,0,20);
//            	 memcpy(wifi[i].single,q,strlen(q));
 //           	 printf ("strlen=======%s\n",q);
            	 strcpy(wifi[i].single,q);
            	 printf ("single=======%s\n",wifi[i].single);
            	 break;
             case 1:
      //         memset((void *)wifi[i].mac,0,40);
//            	 memcpy(wifi[i].mac,q,strlen(q));
            	// printf ("strlen=======%d\n",strlen(q));
//            	 strcpy(wifi[i].mac,test);
            	 strcpy(wifi[i].mac,q);
             	 printf ("mac=======%s\n",wifi[i].mac);

                 break;
             case 2:
    //        	 memset((void *)wifi[i].ssid,0,120);
//            	 memcpy(wifi[i].ssid,q,strlen(q));
//            	 printf ("strlen=======%d\n",strlen(q));
            	 strcpy(wifi[i].ssid,q);
            	 printf ("ssid=======%s\n",wifi[i].ssid);
                 break;
             }

        j++;

        buf=NULL;
    }
        i++;
        buf=NULL;
        if(i>20)
             break;
    }
    pclose(fp);
    int k ;
    for( k=0;k<i;k++)
    {
//    printf("SINGLE:%s\tMAC:%s\tSSID:%s\n",wifi[k].single,wifi[k].mac,wifi[k].ssid);
    }
    printf("wifi_scan<<<---\n");
    return i;
}

void test_event()
{
	network_manage();
	printf("test_event");
}

void network_manage ()
{
	int rec,num,i;
	//rec = network_analysis ();
	if(1)
	{
		stu wifi_list[20];
		num = wifi_scan (wifi_list);
		for(i=0;i<num;i++)
		{
	//		printf("the single ==== %s  \n",wifi_list[i].single);
	//		printf("the ssid ==== %s  \n",wifi_list[i].ssid);
	//		printf("the mac ==== %s  \n",wifi_list[i].mac);
		}
		printf("network_manage\n");
	}

}
